/*
   Copyright 2011 James Bowling

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.armedia.dctm.constraints.module;

import java.util.List;

import com.armedia.dctm.constraints.Constraint;
import com.armedia.dctm.constraints.ForeignKeyConstraintImpl;
import com.armedia.dctm.constraints.ForeignKeyDto;
import com.armedia.dctm.constraints.ForeignKeysForTypeQuery;
import com.armedia.dctm.constraints.NotNullAttrsForTypeQuery;
import com.armedia.dctm.constraints.NotNullConstraintImpl;
import com.documentum.fc.client.IDfPersistentObject;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;

public class ConstraintModuleImpl implements ConstraintModule {
	
	private NotNullAttrsForTypeQuery notNullAttrsQuery = new NotNullAttrsForTypeQuery();
	private ForeignKeysForTypeQuery foreignKeysForTypeQuery = new ForeignKeysForTypeQuery(); 

	public void check(IDfPersistentObject persistentObject) throws DfException {
		final String typeName = persistentObject.getType().getName();
		final List<String> notNullAttributes = notNullAttrsQuery.execute(persistentObject.getObjectSession(), typeName);
		checkNotNulls(persistentObject, notNullAttributes);
		checkForeignKeys(persistentObject, notNullAttributes);
	}

	private void checkForeignKeys(IDfPersistentObject persistentObject,final List<String> notNullAttributes) throws DfException {
		final String typeName = persistentObject.getType().getName();		
		final List<ForeignKeyDto> foreignKeys = foreignKeysForTypeQuery.execute(persistentObject.getObjectSession(), typeName);
		for (ForeignKeyDto foreignKeyDto : foreignKeys) {
			if (foreignKeyDto.getChildAttributes().size() > 1) {
				throw new DfException("Multi-attribute foreign keys not currently suported");
			}
			final String attributeName = foreignKeyDto.getChildAttributes().get(0);
			final String parentTypeName = foreignKeyDto.getParentTypeName();
			final String parentAttributeName = foreignKeyDto.getParentAttributes().get(0);
			boolean mandatory = notNullAttributes.contains(attributeName);
			Constraint foreignKeyConstraint = new ForeignKeyConstraintImpl(typeName, attributeName, parentTypeName, parentAttributeName, mandatory);
			foreignKeyConstraint.check(persistentObject);
		}
		
	}

	private void checkNotNulls(final IDfPersistentObject persistentObject,
		final List<String> notNullAttributes) throws DfException {
		for (String notNullAttribute : notNullAttributes) {
			final String typeName = persistentObject.getType().getName();
			Constraint notNullConstraint = new NotNullConstraintImpl(typeName, notNullAttribute);
			notNullConstraint.check(persistentObject);
		}
	}

	public void checkAll(IDfSession session, String typeName) throws DfException {
		// TODO Auto-generated method stub
		
	}
	
}
